<?php 
require_once 'models/users.php';
require_once 'dbhelper.php';
class user_controller extends dbhelper {

    public function user_login($_username, $_password, $_ck_remember) {
	   $_password = md5($_password);
        $role_ck = 1;
        $query = "SELECT * FROM users WHERE username = '$_username' AND password = '$_password'";
        $result = parent::execQuery($query);
        if ($result && parent::getNumRows($result) > 0) {
            if ($row = parent::fetchArray($result)) {
                $user = new users($row["id"],$row["username"],$row["password"],$row['firstname'],$row['lastname'],$row['sex'],$row["email"],$row['birthday'],$row["sitename"],$row["siteurl"],$row["role"],$row["location"]);
                $_SESSION["user"] = $user;
                $_SESSION["login_time"] = time();
		$_SESSION["username"] = $row['username'];
                if($_ck_remember == "check"){
                    $value1 = $row["username"];
                    $value3 = $row["role"];
                    setcookie("username", $value1, time() + 3600);
                }
                return true;
            }
        } else {
            return false;
        }
    }
    public function logout() {
        $value1 = "log";
        $value2 = "out";
        setcookie("username", $value1, time() + 3600);
        session_destroy();
        //var_dump($_COOKIE["username"]); var_dump($_COOKIE["password"]);  exit;
    }
    public function getAllAccount()
    {
        $users = array();
        $sql = "SELECT * FROM users";
        $result = parent::execQuery($sql);
            while($row = parent::fetchArray($result))
            {
                $user = new users($row["id"],$row["username"],$row["password"],$row['firstname'],$row['lastname'],$row['sex'],$row["email"],$row['birthday'],$row["sitename"],$row["siteurl"],$row["role"],$row["location"]);
                $users[count($users)] = $user;
            }
        return $users;
    }
    public function getAllAccountExceptId($id)
    {
        $users = array();
        $sql = "SELECT * FROM users WHERE NOT id = $id";
        $result = parent::execQuery($sql);
            while($row = parent::fetchArray($result))
            {
                $user = new users($row["id"],$row["username"],$row["password"],$row['firstname'],$row['lastname'],$row['sex'],$row["email"],$row['birthday'],$row["sitename"],$row["siteurl"],$row["role"],$row["location"]);
                $users[count($users)] = $user;
            }
        return $users;
    }
    public function getAccountById($id)
    {
        $id = $id;
        $sql = "SELECT * from users where id = '$id'";
        $result = parent::execQuery($sql);
        if($result && parent::getNumRows($result))
        {
            $row = parent::fetchArray($result);
            $user = new users($row["id"],$row["username"],$row["password"],$row['firstname'],$row['lastname'],$row['sex'],$row["email"],$row['birthday'],$row["sitename"],$row["siteurl"],$row["role"],$row["location"]);
        }
        return $user;
    }
    public function deleteAcountbyId($id)
    {
        $sql = "DELETE FROM users WHERE id = '$id'";
        parent::execQuery($sql);
        if (parent::getAffectedRows() > 0)
            return true;
        return false;
    }
    public function addAcount($account) {
        $account->password = md5($account->password);
        $sql = "INSERT INTO users(username,password,firstname,lastname,sex,email,birthday,sitename,siteurl,role,location) VALUES('$account->username', '$account->password','$account->firstname','$account->lastname','$account->sex', '$account->email','$account->birthday', '$account->sitename', '$account->siteurl', '$account->role', '$account->location')";
        parent::execQuery($sql);
        if (parent::getAffectedRows() > 0)
            return true;
        return false;
    }
    
    public function editAcount($account) {
        $account->password = md5($account->password);
        $sql = "UPDATE users SET password = '$account->password',firstname = '$account->firstname',lastname = '$account->lastname', sex = '$account->sex', email = '$account->email',birthday = '$account->birthday', sitename = '$account->sitename', siteurl = '$account->siteurl', role = '$account->role',location = '$account->location'";
        $sql = $sql . " WHERE id = '$account->id'";
        parent::execQuery($sql);
        if (parent::getAffectedRows() > 0)
            return true;
        return false;
    }
    public function editAcountNotPass($account) {
        $sql = "UPDATE users SET firstname = '$account->firstname',lastname = '$account->lastname', sex = '$account->sex', email = '$account->email',birthday = '$account->birthday', sitename = '$account->sitename', siteurl = '$account->siteurl', role = '$account->role',location = '$account->location'";
        $sql = $sql . " WHERE id = '$account->id'";
        //var_dump($sql);exit;
        parent::execQuery($sql);
        if (parent::getAffectedRows() > 0)
            return true;
        return false;
    }
    
    public function pagging()
    {
        $sql = "SELECT * FROM users";
        $result = parent::execQuery($sql);
        $row = parent::getAffectedRows($result);
        return $row;
    }
    public function isValidEmail($email){
        $atom_re = "[a-z0-9!#$%&'*+\\/=?^_`{|}~-]+";
    $regexp = "/^(" . $atom_re . ")(\\." . $atom_re .
                ")*@([a-z0-9-]+)(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$/i";
	return preg_match($regexp, $email);
    }
    public function checkUser($username){
        $listAccount = $this->getAllAccount();
        foreach($listAccount as $temp){
            if($username == $temp->username) return false;  
        }
        return true;
    }
	public function checkEmail($email){
        $listAccount = $this->getAllAccount();
        foreach($listAccount as $temp){
            if($email == $temp->email) return false;
        }   
        return true;
    }
    public function checkEmailExceptId($email,$id){
        $listAccount = $this->getAllAccountExceptId($id);
        foreach($listAccount as $temp){
            if($email == $temp->email) return false;
        }   
        return true;
    }
	public function countPageUser() {
		$sql = "SELECT * FROM users";
		parent::execQuery($sql);
		$result = parent::getAffectedRows();
		return $result;
	}
    public function pagingUser($startrecord,$r) {
		$userArr = array();
		$startrecord *= $r; 
            $sql = "SELECT * FROM users  LIMIT $startrecord,$r";
            $result = parent::execQuery($sql);
            while($row = parent::fetchArray($result))
            {
                $user = new users($row["id"],$row["username"],$row["password"],$row['firstname'],$row['lastname'],$row['sex'],$row["email"],$row['birthday'],$row["sitename"],$row["siteurl"],$row["role"],$row["location"]);
                $userArr[count($userArr)] = $user;
            }
        return $userArr;
	}
}
?>